package priv.wangy.micro.common.po.echarts;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <a href="https://echarts.apache.org/examples/zh/editor.html?c=pie-simple">...</a>
 *
 * @author zhangjq
 * @since 2024-11-15
 */
@Data
public class PieSimpleChart {

    private Tooltip tooltip;

    private Legend legend;

    private List<Series> series;

    @Data
    public static class Tooltip {

        private String trigger;
    }

    @Data
    public static class Legend {

        private String orient;

        private String left;
    }

    @Data
    public static class Series {

        private String type;

        private String radius;

        private List<ChartData> data;

        private Emphasis emphasis;
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {

        private final List<Map<String, Integer>> pieList = new ArrayList<>();

        public Builder addPie(Map<String, Integer> data) {
            this.pieList.add(data);
            return this;
        }

        public PieSimpleChart build() {
            PieSimpleChart chart = new PieSimpleChart();
            Tooltip tooltip = new Tooltip();
            tooltip.setTrigger("item");
            Legend legend = new Legend();
            legend.setOrient("vertical");
            legend.setLeft("right");
            List<Series> seriesList = this.pieList.stream().map(pie -> {
                Series series = new Series();
                series.setType("pie");
                series.setRadius("50%");
                List<ChartData> data = new ArrayList<>();
                pie.forEach((key, value) -> {
                    ChartData chartData = new ChartData();
                    chartData.setName(key);
                    chartData.setValue(value);
                    data.add(chartData);
                });
                series.setData(data);
                series.setEmphasis(Emphasis.simplePieEmphasis());
                return series;
            }).toList();
            chart.setTooltip(tooltip);
            chart.setLegend(legend);
            chart.setSeries(seriesList);
            return chart;
        }
    }
}
